package com.onlinemusic.controller;

import com.onlinemusic.common.R;
import com.onlinemusic.pojo.request.SongRequest;
import com.onlinemusic.service.SongService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.MultipartConfigFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.util.unit.DataSize;
import org.springframework.util.unit.DataUnit;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.MultipartConfigElement;
import javax.servlet.http.HttpServletResponse;

@RestController
@RequestMapping("song")
public class SongController {

    @Autowired
    private SongService songService;

    /**
     * 添加歌曲
     * @param addSongRequest
     * @param mpfile
     * @return
     */
    @PostMapping("add")
    public R addSong(SongRequest addSongRequest, @RequestParam("file") MultipartFile mpfile) {
        return songService.addSong(addSongRequest, mpfile);
    }

    /**
     * 删除歌曲
     * @param id
     * @return
     */
    @DeleteMapping("delete")
    public R deleteSong(@RequestParam int id) {
        return songService.deleteSong(id);
    }

    /**
     * 获取指定歌手的歌曲
     * @param singerId
     * @return
     */
    @GetMapping("singer/detail")
    public R songOfSingerId(@RequestParam int singerId) {
        return songService.songOfSingerId(singerId);
    }

    /**
     * 更新歌曲信息
     * @param updateSongRequest
     * @return
     */
    @PostMapping("update")
    public R updateSongMsg(@RequestBody SongRequest updateSongRequest) {
        return songService.updateSongMsg(updateSongRequest);
    }

    /**
     * 更新歌曲图片
     * @param urlFile
     * @param id
     * @return
     */
    @PostMapping("img/update")
    public R updateSongPic(@RequestParam("file") MultipartFile urlFile, @RequestParam("id") int id) {
        return songService.updateSongPic(urlFile, id);
    }

    /**
     * 更新歌曲链接也就是内容
     * @param urlFile
     * @param id
     * @return
     */
    @PostMapping("url/update")
    public R updateSongUrl(@RequestParam("file") MultipartFile urlFile, @RequestParam("id") int id) {
        return songService.updateSongUrl(urlFile, id);
    }

    /**
     * 根据songId获取数据 用于展示用户的收藏歌曲
     * @param id
     * @return
     */
    @GetMapping("detail")
    public R songOfId(@RequestParam int id) {
        return songService.songOfId(id);
    }

    /**
     * 获取所有的歌曲
     * @return
     */
    @GetMapping
    public R allSong() {
        return songService.allSong();
    }

    /**
     * 根据歌手名查询歌曲
     * @param name
     * @return
     */
    @GetMapping("singerName/detail")
    public R songOfSingerName(@RequestParam String name) {
        return songService.songOfSingerName('%' + name + '%');
    }

    /**
     * 获取指定歌单的歌曲
     * @param songListId
     * @return
     */
    @GetMapping("songList/{songListId}")
    public R songOfSongListId(@PathVariable("songListId") int songListId) {
        return songService.songOfSongListId(songListId);
    }

    /**
     * 判断歌曲是否已支付
     * @param songId
     * @param userId
     * @return
     */
    @GetMapping("isPay")
    public R isPay(@RequestParam("songId") Integer songId,
                   @RequestParam("userId") Integer userId)  {
        return songService.isPay(songId,userId);
    }

    /**
     * 本地地址下载歌曲
     * @param url
     * @return
     */
    @GetMapping("download")
    public void download(@RequestParam("url") String url, HttpServletResponse response){
        songService.downloadSong(url,response);
    }

    /**
     * oss下载歌曲
     * @param id
     * @return
     * @throws Exception
     */
    @GetMapping("localDownload")
    public R localDownload(@RequestParam("id") Integer id) throws Exception {
        return songService.downloadSong(id);
    }

    /**
     * 推荐歌曲列表
     * @param userId
     * @return
     */
    @GetMapping("recommend/{userId}")
    public R recommend(@PathVariable(value = "userId", required = false) Integer userId) {
        return songService.getRecommendSongList(userId);
    }
}
